<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */

$LANGUAGE_LIST = array();
$defs = simplexml_load_file(ROOT_PATH."lang/languages.xml");

foreach($defs->lang as $def) {
	$l = array("value" => $def->value,"name" => $def->name);
	array_push($LANGUAGE_LIST,$l);
}

$_AL=strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']);

$_ALS = strtok($_AL, ",");
$_ASR=array();

while ($_ALS !== false) {
	array_push($_ASR, $_ALS);
    $_ALS = strtok(",");
}

$lset=false;

foreach ($_ASR as &$value) {
	$v=strpos($value,";");
	if ($v!='') {
		$_AS = substr($value,0,$v);
	} else {
		$_AS = $value;
	}		
	
	$_AS = str_replace("-","_",$_AS);
	$f = ROOT_PATH."lang/lang_".$_AS.".inc";
	
	if (file_exists($f)) {
		require_once($f);
		$lset=true;
		break;
	} elseif(strpos($_AS,"_")!==false) {
		$_AS = substr($_AS,0,strpos($_AS,"_"));
	}
}

if (!$lset) {
	require_once(ROOT_PATH."lang/lang_".DEFAULT_LANG.".inc");
}

function updateLang($lang) {
	$f = ROOT_PATH."lang/lang_".strtolower($lang).".inc";
	if (file_exists($f)) {
		unset($LANG);
		require($f);
	}		
} 
?>
